package com.sjcq.demoexample.controller;

import com.sjcq.demoexample.aop.MyLog;
import com.sjcq.demoexample.entity.User;
import com.sjcq.demoexample.mapper.SysLogMapper;
import com.sjcq.demoexample.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SysLogMapper sysLogMapper;

    @RequestMapping("login")
    public String login(User user,Model model){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());

        try {
            subject.login(token);

            model.addAttribute("msg","欢迎您:"+user.getUsername()+",");
            model.addAttribute("token",token);
            return "index";
        } catch (UnknownAccountException e){
            e.printStackTrace();
            model.addAttribute("msg","账号不存在!");
            return "login";
        } catch (IncorrectCredentialsException e){
            e.printStackTrace();
            model.addAttribute("msg","密码错误!!!");
            return "login";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            model.addAttribute("msg", "各种异常!!");
            return "login";
        }
    }

    @MyLog(value = "查询")
    @RequestMapping("myself")
    public String myself(Model model){
        return "myself";
    }

    // 注销
    @RequestMapping("/logOut")
    public String logOut(){
        SecurityUtils.getSubject().logout();
        return "login";
    }

    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("index")
    public String index(){
        return "index";
    }


    @RequestMapping("page2")
    public String page2(){
        return "page2";
    }

    @RequiresPermissions(value = {"per:root","per:admin"},logical = Logical.OR)
    @RequestMapping("userList")
    public String userList(Model model){
        List<User> allUser = userMapper.findAllUser();
        model.addAttribute("userList", allUser);
        return "alllUser";
    }

    @RequiresPermissions(value = {"per:root","per:admin"},logical = Logical.OR)
    @MyLog(value = "根据ID修改用户信息")
    @RequestMapping("update/updateById/{id}")
    public String updateById(@PathVariable Integer id,User user){
        userMapper.updateByPrimaryKey(user);
        return "redirect:/userList";
    }

    @RequiresPermissions(value = {"per:root","per:admin"},logical = Logical.OR)
    @RequestMapping("update/{id}")
    public String updateById(@PathVariable Integer id,User user,Model model){
        User user1 = userMapper.selectByPrimaryKey(id);
        model.addAttribute("user", user1);
        return "update";
    }

    // 无权访问
    @MyLog(value = "越权访问!")
    @RequestMapping("Unauthorized")
    public String Unauthorized(){
        return "Unauthorized";
    }

    @RequestMapping("/")
    public String toindex() {
        return "/toLogin";
    }

    @RequestMapping("iframe")
    public String testhtml(){
        return "iframe";
    }
}